<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class AppController extends CommonController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = D('App');
    }
    
    /**
     * 
    * 应用列表
    * @author wang
    * @param 
    */
    public function appList()
    {
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $app = I('app');
        if(!empty($app)) $where['app|description'] = array('like','%'.$app.'%');
        $result = $this->model->getList($where,$sort." ".$order);      
        foreach ($result as $k => &$v) {
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }


    /**
    * 删除app
    * @author wang
    * @param 
    */
    public function delApp(){
        $appid = I('appid');
        if($this->model->where("appid='$appid'")->delete()){
            $result = array(
                'sta' => 1, 
                'msg' => '删除成功'
            );
        }else{
            $result = array(
                'sta' => 0, 
                'msg' => '删除失败'
            ); 
        }
        $this->ajaxReturn($result);
    }


    /**
    * 查看app详情
    * @author wang
    * @param 
    */
    public function appDetial(){
        $appid = I('appid');
        $data = M('app')
                ->where("appid='$appid'")
                ->find();
        $data['orderid']=(int)$data['orderid'];
        $data['createtime']=date("Y-m-d H:i:s",$data['createtime']);
        $data['updatetime']=date("Y-m-d H:i:s",$data['updatetime']);
        $this->assign("data",$data);
        $this->display('detial');

    }

    /**
    * 修改内容
    * @author wang
    * @param 
    */
    public function editImg(){
        $data = I();
        $data['admin'] = session('admin');
        $data['updatetime'] = time();
        $appid = $data['appid'];
        $icon = $this->uploadImg();
        if($icon){
            $data['icon']= current($icon['icon']);
        }
        if($appid){
            if(M('app')->where("appid='$appid'")->save($data)){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }else{
            $data['admin'] = session('admin');
            $data['createtime'] = time();
            // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
            if(M('app')->add($data)){
                $this->ajaxReturn(2);
            }else{
                $this->ajaxReturn(0);
            }
        }



    }



}